package LeetcodeTest_thirty_eight;

//395. 至少有K个重复字符的最长子串
public class longestSubstring
{
    public static void main(String[] args)
    {
        String s="ababacb";
        System.out.println(solution.longestSubstring(s,2));
    }
}
class solution
{
    public static int longestSubstring(String s, int k)
    {
        if(s.length()==0||s==null||s.length()<k)
            return 0;
        int[] count=new int[26];
        int maxLength=0;
        for(int i=0;i<s.length();i++)
        {
            char ch=s.charAt(i);
            int index=ch-'a';
            count[index]++;
        }
        boolean flag=true;
        for(int i=0;i<s.length();i++)
        {
            if (count[s.charAt(i) - 'a'] < k)
                flag = false;
        }
        if(flag)
            return s.length();
        int i,j;
        i=j=0;
        while(j<s.length())
        {
            if(count[s.charAt(j)-'a']<k)
            {
                maxLength = Math.max(maxLength, longestSubstring(s.substring(i, j), k));
                i=j+1;
            }
            j++;
        }
        maxLength=Math.max(maxLength,longestSubstring(s.substring(i,j),k));
        return maxLength;
    }
}